﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Yang.DataBase;

public partial class WebServices_getStatQuarter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "{ \"Details\":[{";

        string machineCode = Request.QueryString[0].ToString();



        string strDate = DateTime.Now.Date.ToString();//记录当前日期
        string startDate = string .Empty ;
        int year = DateTime .Now .Year ;
        int month = DateTime.Now.Month;
        if (month < 4)
            startDate = new DateTime(year, 1, 1).ToString();
        else if (month >= 4 && month < 7)
            startDate = new DateTime(year, 4, 1).ToString();
        else if (month >= 7 && month < 10)
            startDate = new DateTime(year, 7, 1).ToString();
        else
            startDate = new DateTime(year, 10, 1).ToString();



        int nPoints = 0;//统计各作业类型的点数

        string strSql = "select distinct 作业类型 from 作业统计 where 日期 >= '" + startDate + "' and 日期 < '" + strDate + "' and 牌照号 = '" + machineCode + "'" ;
        DataSet ds = Database .Query (strSql );
        int nWorks = ds.Tables[0].Rows.Count;
        AgriMachineWork[] works = new AgriMachineWork[nWorks];
        for (int i = 0; i < nWorks; i++)
        {
            works[i] = new AgriMachineWork();
            works[i].WorkName = ds.Tables[0].Rows[i][0].ToString();
            strSql = "select  作业量,作业收入 from 作业统计" + 
                " where 日期 >= '" + startDate + 
                "' and 日期 < '" + strDate + 
                "' and 牌照号 = '" + machineCode + 
                "' and 作业类型 = '" + works[i].WorkName + "'";
            ds.Clear();
            ds = Database.Query(strSql);
            nPoints = ds.Tables[0].Rows.Count;
            double workAmount = 0.0;
            double workMoney = 0.0;

            for (int j = 0; j < nPoints; j++)
            {
                workAmount += double.Parse(ds.Tables[0].Rows[i][0].ToString());
                workMoney += double.Parse(ds.Tables[0].Rows[i][1].ToString());
            }
            //works [i].WorkAmount =
            resultJson += "\"WorkName\":\"" + works[i].WorkName + "\"," + "\"WorkAmount\":\"" + workAmount + "\"," + "\"WorkMoney\":\"" + workMoney + "\"},";

        }
        if (resultJson.IndexOf("Error") < 0)
        {
            resultJson = resultJson.Substring(0, resultJson.Length - 1);
            resultJson += "]}";
        }
        Response.Write(resultJson);


    }
}
